package com.abc.recycling.controller;

import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.RecyclingStationService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/stations")
@RequiredArgsConstructor
public class RecyclingStationController {

    private final RecyclingStationService stationService;

    @GetMapping
    public String listStations(Model model, 
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestHeader(value = "X-Requested-With", required = false) String requestedWith) {
        
        Pageable pageable = PageRequest.of(page, size);
        Page<RecyclingStation> stations = stationService.searchStations(null, pageable);
        model.addAttribute("stations", stations);
        
        // 如果是AJAX请求或是普通用户访问，返回普通列表页面
        if ("XMLHttpRequest".equals(requestedWith) || !isAdminPath()) {
            return "station/list";
        }
        
        // 否则返回管理员页面
        return "admin/station/list";
    }

    @GetMapping("/api/all")
    @ResponseBody
    public Page<RecyclingStation> getAllStations(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return stationService.searchStations(null, PageRequest.of(page, size));
    }

    @GetMapping("/api/active")
    @ResponseBody
    public Page<RecyclingStation> getActiveStations(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return stationService.searchStations(null, PageRequest.of(page, size));
    }

    @GetMapping("/api/{id}")
    @ResponseBody
    public RecyclingStation getStation(@PathVariable Long id) {
        return stationService.findById(id);
    }

    @PostMapping("/api")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public RecyclingStation createStation(@RequestBody RecyclingStation station) {
        stationService.create(station);
        return station;
    }

    @PutMapping("/api/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public RecyclingStation updateStation(@PathVariable Long id, @RequestBody RecyclingStation station) {
        station.setId(id);
        stationService.update(station);
        return station;
    }

    @DeleteMapping("/api/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public void deleteStation(@PathVariable Long id) {
        stationService.delete(id);
    }

    @PutMapping("/api/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public void updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        stationService.updateStatus(id, status);
    }

    @GetMapping("/api/search")
    @ResponseBody
    public Page<RecyclingStation> searchStations(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return stationService.searchStations(keyword, PageRequest.of(page, size));
    }

    @GetMapping("/api/nearby")
    @ResponseBody
    public List<RecyclingStation> getNearbyStations(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam Integer radius) {
        return stationService.findNearbyStations(latitude, longitude, radius);
    }
    
    private boolean isAdminPath() {
        return true; // TODO: 根据实际路径判断是否是管理员路径
    }
} 